//
//  SLSettingWeightView.m
//  BXlive
//
//  Created by sweetloser on 2020/8/27.
//  Copyright © 2020 cat. All rights reserved.
//

#import "SLSettingWeightView.h"
#import <MJRefresh/MJRefresh.h>
#import <SLDeveloperTools/SLDeveloperTools.h>
#import <Masonry/Masonry.h>

@interface SLSettingWeightView()<UIPickerViewDelegate,UIPickerViewDataSource>

@property(nonatomic,strong)UIPickerView *pickView;

@end

@implementation SLSettingWeightView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
        
    }
    
    return self;
}

-(void)setupUI{
    self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, __ScaleWidth(321), __ScaleWidth(300))];
    [self addSubview:self.contentView];
    
    self.contentView.center = CGPointMake(self.mj_w/2, self.mj_h/2);
    self.contentView.backgroundColor = sl_BGColors;
    self.contentView.layer.masksToBounds = YES;
    self.contentView.layer.cornerRadius = 12;
    
    
    UILabel *titleL = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:NSLocalizedString(@"选择体重", nil) Font:SLBFont(16) TextColor:sl_textColors];
    titleL.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:titleL];
    
    [titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(__ScaleWidth(30));
        make.height.mas_equalTo(__ScaleWidth(22));
    }];
    
    //    按钮
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:cancelBtn];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(__ScaleWidth(28));
        make.width.mas_equalTo(__ScaleWidth(122));
        make.height.mas_equalTo(__ScaleWidth(44));
        make.bottom.mas_equalTo(-__ScaleWidth(30));
    }];
    cancelBtn.backgroundColor = sl_subBGColors;
    cancelBtn.layer.masksToBounds = YES;
    cancelBtn.layer.cornerRadius = __ScaleWidth(22);
    [cancelBtn setTitle:NSLocalizedString(@"取消", nil) forState:BtnNormal];
    [cancelBtn sl_setTitleColor:sl_textColors];
    cancelBtn.titleLabel.font = SLBFont(14);
    [cancelBtn addTarget:self action:@selector(hiddenView) forControlEvents:BtnTouchUpInside];
    
    
    UIButton *determineBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:determineBtn];
    [determineBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(__ScaleWidth(-28));
        make.width.mas_equalTo(__ScaleWidth(122));
        make.height.mas_equalTo(__ScaleWidth(44));
        make.bottom.mas_equalTo(-__ScaleWidth(30));
    }];
    determineBtn.backgroundColor = sl_normalColors;
    determineBtn.layer.masksToBounds = YES;
    determineBtn.layer.cornerRadius = __ScaleWidth(22);
    [determineBtn setTitle:NSLocalizedString(@"确定", nil) forState:BtnNormal];
    [determineBtn sl_setTitleColor:sl_whiteTextColors];
    determineBtn.titleLabel.font = SLBFont(14);
    [determineBtn addTarget:self action:@selector(determineBtnAction) forControlEvents:BtnTouchUpInside];
    
    
    
    
    _pickView = [[UIPickerView alloc] initWithFrame:CGRectZero];
    
    [self.contentView addSubview:_pickView];
    [_pickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(__ScaleWidth(150));
        make.width.mas_equalTo(__ScaleWidth(70));
    }];
    _pickView.delegate = self;
    WS(weakSelf);
    UILabel *unitL = [UILabel createLabelWithFrame:CGRectZero BackgroundColor:SLClearColor Text:@"kg" Font:SLPFFont(16) TextColor:sl_textSubColors];
    [self.contentView addSubview:unitL];
    [unitL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf.pickView);
        make.left.equalTo(weakSelf.pickView.mas_right).offset(__ScaleWidth(20));
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(__ScaleWidth(22));
    }];
    
}

-(void)determineBtnAction{
    NSLog(NSLocalizedString(@"确定", nil));
    WS(weakSelf);
    [[NewHttpManager sharedNetManager] POST:@"s=User.saveInfo" parameters:@{@"weight":self.selectedWeight} success:^(id  _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 0) {
//            更新成功
            BXLiveUser *user = [BXLiveUser currentBXLiveUser];
            user.weight = responseObject[@"data"][@"weight"];
            [BXLiveUser setCurrentBXLiveUser:user];
            if (weakSelf.weightComplateBlock) {
                weakSelf.weightComplateBlock(self.selectedWeight);
            }
        }
        
    } failure:^(NSError * _Nonnull error) {
        [BGProgressHUD showInfoWithMessage:NSLocalizedString(@"设置失败", nil)];
    }];
    
    
    [self hiddenView];
    
    
}

-(void)defaultRow:(NSString *)row{
    self.selectedWeight =  row;
    [_pickView selectRow:[self.selectedWeight integerValue] inComponent:0 animated:NO];
}


#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;{
    return 200;
}
#pragma mark - UIPickerViewDelegate
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [NSString stringWithFormat:@"%ld",(long)row];
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;{
    return __ScaleWidth(50);
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSLog(@"aaaa%ld",row);
    self.selectedWeight = [NSString stringWithFormat:@"%ld",(long)row];
}

@end
